package com.magold.game.factory;

import com.magold.game.level.MagoldLevel;
import com.magold.io.XMLReader;
import com.magold.level.ILevel;
import com.magold.logic.ISpriteLogic;
import com.magold.logic.item.BaseItem;
import com.magold.logic.item.ChickenLeg;
import com.magold.logic.item.Coin;
import com.magold.logic.item.Cola;
import com.magold.logic.item.Mushroom;
import com.magold.logic.item.RedTea;
import com.magold.logic.magold.ItemsSupports;

public abstract class ItemFactory extends BaseLogicFactory {
	protected ItemsSupports mTarget;
	
	public ItemFactory(Class<? extends BaseItem> clazz) {
		super(clazz.getSimpleName());
	}

	@Override
	public boolean onLoadEntity(XMLReader pReader, ILevel pLoadingLevel) {
		mTarget = (ItemsSupports) pLoadingLevel.getAttributeValue(MagoldLevel.MAGOLD_KEY);
		return super.onLoadEntity(pReader, pLoadingLevel);
	}
	
	public static ItemFactory forChicken() {
		return new ItemFactory(ChickenLeg.class) {
			@Override
			protected ISpriteLogic createSafely(XMLReader pReader) {
				return new ChickenLeg(mTarget);
			}
		};
	}
	
	public static ItemFactory forCoin() {
		return new ItemFactory(Coin.class) {
			@Override
			protected ISpriteLogic createSafely(XMLReader pReader) {
				return new Coin(mTarget, mTarget);
			}

			@Override
			public boolean onLoadEntity(XMLReader pReader, ILevel pLoadingLevel) {
				boolean ret = super.onLoadEntity(pReader, pLoadingLevel);
				int prev = (Integer) pLoadingLevel.getAttributeValue(MagoldLevel.TOTAL_COIN_KEY);
				pLoadingLevel.setAttributes(MagoldLevel.TOTAL_COIN_KEY, prev+1);
				return ret;
			}
		};
	}
	
	public static ItemFactory forCola() {
		return new ItemFactory(Cola.class) {
			@Override
			protected ISpriteLogic createSafely(XMLReader pReader) {
				return new Cola(mTarget);
			}
		};
	}
	
	public static ItemFactory forMushroom() {
		return new ItemFactory(Mushroom.class) {
			@Override
			protected ISpriteLogic createSafely(XMLReader pReader) {
				return new Mushroom(mTarget);
			}
		};
	}
	
	public static ItemFactory forRedTea() {
		return new ItemFactory(RedTea.class) {
			@Override
			protected ISpriteLogic createSafely(XMLReader pReader) {
				return new RedTea(mTarget);
			}
		};
	}

}
